package com.neuqsoft.ymtdemo.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.neuqsoft.ymtdemo.R;
import com.neuqsoft.ymtdemo.bean.MoudleBean;
import com.neuqsoft.ymtdemo.utils.RequestUtil;

import java.util.Map;

import essclib.pingan.ai.request.biap.Biap;
import essclib.pingan.ai.request.biap.net.LogUtils;
import hc.mhis.paic.com.essclibrary.EsscSDK;
import hc.mhis.paic.com.essclibrary.listener.ESSCCallBack;

/**
 * <pre>
 *     author : wulh
 *     e-mail : wulh@neuqsoft.com
 *     time   : 2020/03/19
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class LoginEsscActivity extends AppCompatActivity implements ESSCCallBack {
    private EditText aac002;
    private String aac002Str;
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            String strMsg = (String) msg.obj;
            Toast.makeText(LoginEsscActivity.this, strMsg, Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_essc_login);
        aac002 = findViewById(R.id.aac002);
    }

    public void startLogin(View view) {
        aac002Str = aac002.getText().toString().trim();
        if (TextUtils.isEmpty(aac002Str)) {
            Toast.makeText(this, "社会保障号不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        RequestUtil.getApply(aac002Str, new RequestUtil.CallBackNet() {
            @Override
            public void onSuccess(String data) {
                Map<String, String> result = JSONObject.parseObject(data, Map.class);
                String aac002Str = result.get("idcard");
                String aac003Str = result.get("userName");
                String aac067Str = result.get("aac067");
                String signNo = result.get("signNo");
                sign(aac002Str, aac003Str, aac067Str, signNo);
            }

            @Override
            public void onFail(String data) {
                Message message = new Message();
                message.obj =data;
                mHandler.sendMessageDelayed(message, 100);
            }
        });
    }

    private void sign(String aac002Str, String aac003Str, String aac067Str, String signNo) {
        RequestUtil.sign(aac002Str, aac003Str, "", signNo, aac067Str, "", "", "1", "", "", new RequestUtil.CallBackNet() {
            @Override
            public void onSuccess(String data) {
                EsscSDK.getInstance().startSdk(LoginEsscActivity.this, Biap.getInstance().getPwdValidate() + "?" + data, LoginEsscActivity.this);
            }

            @Override
            public void onFail(String data) {
                Message message = new Message();
                message.obj =data;
                mHandler.sendMessageDelayed(message, 100);
            }
        });
    }

    @Override
    public void onESSCResult(String data) {
        //TODO SDK回调结果
        Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
        LogUtils.e("result", data);
        //此处为方便测试，保存用户信息到本地
        final MoudleBean bean = JSONObject.parseObject(data, MoudleBean.class);
        if (!TextUtils.isEmpty(bean.getActionType()) && bean.getActionType().equals("009")) {
            //关闭电子社保卡sdk
            EsscSDK.getInstance().closeSDK();
            //用拿到的信息去做登录操作
            RequestUtil.login(bean.getBusiSeq(), bean.getSignNo(), new RequestUtil.CallBackNet() {
                @Override
                public void onSuccess(String data) {
                    //登录成功去首页
                    Intent intent = new Intent(LoginEsscActivity.this, YouAppHomeActivity.class);
                    intent.putExtra("message", data);
                    startActivity(intent);
                }

                @Override
                public void onFail(String data) {

                }
            });
        }
    }
}
